from django.shortcuts import render

# Create your views here.
from rest_framework.viewsets import GenericViewSet
# GenericViewSet 重写了 as_view，路由配置变样了
from rest_framework.mixins import CreateModelMixin, RetrieveModelMixin, UpdateModelMixin

from api import models, ser


class RegisterView(GenericViewSet, CreateModelMixin, RetrieveModelMixin, UpdateModelMixin):
    queryset = models.User.objects.all()
    serializer_class = ser.UserModelSerializer

    # 假设get请求和post请求，用的序列化类不一样，如何处理
    # 重写 get_serializer_class 方法 ，返回什么，就用什么序列化类
    def get_serializer_class(self):
        print(self.action)
        if self.action == 'create':
            return ser.UserModelSerializer  # 注册用的序列化类
        elif self.action == 'retrieve':
            return ser.UserReadOnlyModelSerializer  # 查询一个用的这个序列化类
        elif self.action == 'update':
            return ser.UserImageModelSerializer